﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory.Faction

    ''' <summary>
    ''' A class that caches the factions available on the Armory
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Factions
        Inherits WowArmoryData

        Private fsPage As New Schema.page

        ''' <summary>
        ''' The URL of the faction data
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Return "http://www.wowarmory.com/data/factionStrings.xml"
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Returns a list of available factions
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Factions As Collection(Of Faction)
            Get
                If fsPage Is Nothing Then
                    Return New Collection(Of Faction)
                End If
                Return (From a In fsPage.factions(0).faction Select New Faction(a.id, a.name, CType(a.release, Enums.ReleaseType))).ToCollection
            End Get
        End Property

        ''' <summary>
        ''' Constructor that retrieves the fuction data from either the armory or the local cache
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            If Cache.Cache("LibWowArmory.Factions") Is Nothing Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(fsPage.GetType)

                Using srXML As New StringReader(MyBase.Data)
                    fsPage = CType(xsSerializer.Deserialize(srXML), Schema.page)
                End Using

                Cache.Cache.Insert("LibWowArmory.Factions", fsPage, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.NotRemovable, Nothing)
            Else
                fsPage = CType(Cache.Cache("LibWowArmory.Factions"), Schema.page)
            End If
        End Sub

    End Class

End Namespace